package user

import (
	"context"
	"fmt"

	"mydemo/api/rewark/internal/svc"
	"mydemo/api/rewark/internal/types"

	"mydemo/api/rewark/internal/model"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewGetUserLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserLogic {
	return &GetUserLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *GetUserLogic) GetUser(req *types.GetUserInput) (resp *types.GetUserOutput, err error) {
	out := &types.GetUserOutput{}
	userDao := &model.User{Id: int64(req.Id)}
	if err := userDao.First(l.svcCtx.Db); err != nil {
		return out, err
	}
	out.Id = uint(userDao.Id)
	out.Name = userDao.Name
	redisKey := "zero-user_" + fmt.Sprintf("%v", out.Id)
	l.svcCtx.Redis.Setex(redisKey, out.Name, 60)
	return out, nil
}
